package com.wjb.xietong.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.RadioButton;

public class CVRadioButton extends RadioButton {

    public CVRadioButton(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
    }

    public CVRadioButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CVRadioButton(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Drawable[] drawables = getCompoundDrawables();
        if (drawables != null) {
        	Drawable drawableLeft = drawables[0];
        	Drawable drawableTop = drawables[1];
            Drawable drawableRight = drawables[2];
            Drawable drawableBottom = drawables[3];

            if (drawableLeft != null || drawableRight != null) {
                float textWidth = getPaint().measureText(getText().toString());
                int drawablePadding = getCompoundDrawablePadding();
                int drawableWidth = 0;
                if (drawableLeft != null)
                    drawableWidth = drawableLeft.getIntrinsicWidth();
                else if (drawableRight != null) {
                    drawableWidth = drawableRight.getIntrinsicWidth();
                }
                float bodyWidth = textWidth + drawableWidth + drawablePadding;
                canvas.translate((getWidth() - bodyWidth) / 2, 0);
            }
        	
            if (drawableTop != null || drawableBottom != null) {
                float textHeight = getPaint().getTextSize();
                int drawablePadding = getCompoundDrawablePadding();
                int drawableHeight = 0;
                if (drawableTop != null)
                	drawableHeight = drawableTop.getIntrinsicHeight();
                else if (drawableBottom != null) {
                	drawableHeight = drawableBottom.getIntrinsicHeight();
                }
                float bodyHeight = textHeight + drawableHeight + drawablePadding;
                canvas.translate(0, (getHeight() - bodyHeight) / 2);
            }
        }
        super.onDraw(canvas);
    }
}